﻿using HDF5CSharp;
using System;

internal class Program
{
    private static void Main(string[] args)
    {
        string filename = "testdemo.h5";

        // 创建 HDF5 文件
        long fileId = Hdf5.CreateFile(filename);
        if (fileId < 0)
        {
            Console.WriteLine("Failed to create HDF5 file.");
            return;
        }

        // 创建组 group1
        long groupId1 = Hdf5.CreateOrOpenGroup(fileId, "/group1");
        if (groupId1 < 0)
        {
            Console.WriteLine("Failed to create group1.");
            Hdf5.CloseFile(fileId);
            return;
        }

        // 为组 group1 添加属性
        Hdf5.WriteAttribute(groupId1, "group-attribute", "This is a group attribute");

        // 创建数据集 dataset1 在组 group1 中
        int[] data1 = new int[1000];
        for (int i = 0; i < 1000; i++)
        {
            data1[i] = i + 1; // 填充数据
        }
        Hdf5.WriteDataset(groupId1, "dataset1", data1);
        Hdf5.WriteDataset(groupId1, "dataset2", data1);
        // 为数据集 dataset1 添加属性
        long datasetId = Hdf5.OpenDatasetIfExists(groupId1, "dataset1", "");
        Hdf5.WriteAttribute(datasetId, "dataset-attribute", "This is a dataset attribute");

        // string path = "/group1/dataset1";
        // string attributeValue = "test";
        // var res = Hdf5Utils.WriteAttributeByPath(filename, path, "VALID", attributeValue);
        // Console.WriteLine(res.ToString());

        // 创建组 group2
        long groupId2 = Hdf5.CreateOrOpenGroup(fileId, "/group2");
        if (groupId2 < 0)
        {
            Console.WriteLine("Failed to create group2.");
            Hdf5.CloseGroup(groupId1);
            Hdf5.CloseFile(fileId);
            return;
        }

        // 为组 group2 添加属性
        Hdf5.WriteAttribute(groupId2, "group-attribute", "This is group2 attribute");

        int row = 51200;
        int col = 2;
        // 创建数据集 dataset2 在组 group2 中
        double[,] data2 = new double[row, col];
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                data2[i, j] = (i + 1) * (j + 1); // 填充数据
            }
        }
        //Hdf5.WriteDataset(groupId2, "dataset2", data2);

        double[,] data2Append = new double[row, col];
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                data2Append[i, j] = (i + 1) * (j + 1) + 100; // 填充数据
            }
        }
        using (var chunkedDset = new ChunkedDataset<double>("dataset2", groupId2, data2))
        {
            for (int i = 0; i < 100; i++)
            {
                chunkedDset.AppendDataset(data2Append);
            }
        }

        

        // 为数据集 dataset2 添加属性
        long dataset2Id = Hdf5.OpenDatasetIfExists(groupId2, "dataset2", "");
        Hdf5.WriteAttribute(dataset2Id, "dataset-attribute", 3000);

        // 关闭资源
        Hdf5.CloseGroup(groupId2);
        Hdf5.CloseGroup(groupId1);
        Hdf5.CloseFile(fileId);

        Console.WriteLine("HDF5 file created successfully.");
    }
}